package com.nisum.mocksimulator.externalize;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class ExternalizableMessageReader {
	@Value("#{null}")
	private String directory;
	
	public Object read(String objectName) {
		FileInputStream fis = null;
		ObjectInputStream in = null;
		Object obj = null;
		try {
			fis = new FileInputStream(new File(this.directory,objectName));
			in = new ObjectInputStream(fis);
			obj = in.readObject();
		} catch (IOException e) {
			System.out.println(e.getMessage());
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally {
			if(fis != null && in != null){
				try {
					in.close();
					fis.close();
				} catch (IOException e) {
					System.out.println(e.getMessage());
					return false;
				}
			}
		}
		return obj;
	}
	
	public String getDirectory() {
		return this.directory;
	}

	public void setDirectory(String directory) {
		this.directory = directory;
	}
	
}
